package com.panda.system.controller;

import com.panda.common.annotation.Log;
import com.panda.common.core.controller.BaseController;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.core.page.TableDataInfo;
import com.panda.common.enums.BusinessType;
import com.panda.common.utils.poi.ExcelUtil;
import com.panda.system.common.constant.EnumConstants;
import com.panda.system.domain.TPandaRebatesTask;
import com.panda.system.service.ITPandaRebatesTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 返款任务看板Controller
 * 
 * @author panda
 * @date 2021-02-28
 */
@RestController
@RequestMapping("/system/RebatesTask")
public class TPandaRebatesTaskController extends BaseController
{
    @Autowired
    private ITPandaRebatesTaskService tPandaRebatesTaskService;

    /**
     * 查询返款任务看板列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TPandaRebatesTask tPandaRebatesTask)
    {
        startPage();
        if(tPandaRebatesTask !=null &&tPandaRebatesTask.getEndTime() != null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(tPandaRebatesTask.getEndTime());
            calendar.add(Calendar.DATE, 1);
            tPandaRebatesTask.setEndTime(calendar.getTime());
        }
        List<TPandaRebatesTask> list = tPandaRebatesTaskService.selectTPandaRebatesTaskList(tPandaRebatesTask);
        return getDataTable(list);
    }

    /**
     * 导出返款任务看板列表
     */
    @Log(title = "返款任务看板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPandaRebatesTask tPandaRebatesTask)
    {
        List<TPandaRebatesTask> list = tPandaRebatesTaskService.selectTPandaRebatesTaskList(tPandaRebatesTask);
        ExcelUtil<TPandaRebatesTask> util = new ExcelUtil<TPandaRebatesTask>(TPandaRebatesTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取返款任务看板详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tPandaRebatesTaskService.selectTPandaRebatesTaskById(id));
    }

    /**
     * 新增返款任务看板
     */
    @Log(title = "返款任务看板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPandaRebatesTask tPandaRebatesTask)
    {
        return AjaxResult.success(tPandaRebatesTaskService.insertTPandaRebatesTask(tPandaRebatesTask));
    }

    /**
     * 修改返款任务看板
     */
    @Log(title = "返款任务看板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPandaRebatesTask tPandaRebatesTask)
    {
        return toAjax(tPandaRebatesTaskService.updateTPandaRebatesTask(tPandaRebatesTask));
    }

    /**
     * 删除返款任务看板
     */
    @Log(title = "返款任务看板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tPandaRebatesTaskService.deleteTPandaRebatesTaskByIds(ids));
    }

    /**
     * 已返款
     */
    @Log(title = "返款任务看板", businessType = BusinessType.UPDATE)
    @GetMapping("/beRebates/{id}")
    public AjaxResult beRebates(@PathVariable("id") Long id)
    {
        TPandaRebatesTask tPandaRebatesTask = new TPandaRebatesTask();
        tPandaRebatesTask.setId(Long.valueOf(id));
        tPandaRebatesTask.setStatus(EnumConstants.RebatesTypeTaskStatus.BE_Rebates.getCode());
        tPandaRebatesTask.setUpdateTime(new Date());
        return toAjax(tPandaRebatesTaskService.updateTPandaRebatesTask(tPandaRebatesTask));
    }
}
